package com.mock.constructor;

import com.mock.BasePowerMock;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.math.BigDecimal;

/**
 * @Descripton mock构造方法
 * @Author liutianlong
 * @Date 2022/4/26 Version 1.0
 **/
@PrepareForTest({OrderService.class})
public class MockConstructor extends BasePowerMock {

    @Mock
    private Pay pay;

    @Test
    public void mockPay() throws Exception {
        String id = "001";
        BigDecimal amount = new BigDecimal("1000");
        PowerMockito.whenNew(Pay.class).withArguments(id,amount).thenReturn(pay);
        PowerMockito.doNothing().when(pay).trans();
        OrderService orderService = new OrderService();
        // 构建mock对象的参数与此处传入的参数要保持一致，否则运行时会出现mock的对象为空
        orderService.pay(id,amount);
//        orderService.pay("id",amount);
        Mockito.verify(pay).trans();
    }
}
